package com.yic.module.base.controller.admin.item;

import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.base.dal.dataobject.itemCategory.ItemCategoryDO;
import com.yic.module.base.dal.dataobject.unit.UnitDO;
import com.yic.module.base.service.itemCategory.ItemCategoryService;
import com.yic.module.base.service.unit.UnitService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;

import static com.yic.framework.common.util.collection.CollectionUtils.convertList;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.item.vo.*;
import com.yic.module.base.dal.dataobject.item.ItemDO;
import com.yic.module.base.convert.item.ItemConvert;
import com.yic.module.base.service.item.ItemService;

@Tag(name = "管理后台 - 产品通用信息")
@RestController
@RequestMapping("/base/item")
@Validated
public class ItemController {

    @Resource
    private ItemService itemService;
    @Resource
    private ItemCategoryService itemCategoryService;
    @Resource
    private UnitService unitService;

    @PostMapping("/create")
    @Operation(summary = "创建产品通用信息")
    @PreAuthorize("@ss.hasPermission('base:item:create')")
    public CommonResult<Long> createItem(@Valid @RequestBody ItemCreateReqVO createReqVO) {
        return success(itemService.createItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品通用信息")
    @PreAuthorize("@ss.hasPermission('base:item:update')")
    public CommonResult<Boolean> updateItem(@Valid @RequestBody ItemUpdateReqVO updateReqVO) {
        itemService.updateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品通用信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:item:delete')")
    public CommonResult<Boolean> deleteItem(@RequestParam("id") Long id) {
        itemService.deleteItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品通用信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:item:query')")
    public CommonResult<ItemRespVO> getItem(@RequestParam("id") Long id) {
        ItemDO item = itemService.getItem(id);
        return success(ItemConvert.INSTANCE.convert(item));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品通用信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:item:query')")
    public CommonResult<List<ItemRespVO>> getItemList(@RequestParam("ids") Collection<Long> ids) {
        List<ItemDO> list = itemService.getItemList(ids);
        return success(ItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品通用信息分页")
    @PreAuthorize("@ss.hasPermission('base:item:query')")
    public CommonResult<PageResult<ItemRespVO>> getItemPage(@Valid ItemPageReqVO pageVO) {
        PageResult<ItemDO> pageResult = itemService.getItemPage(pageVO);
        // 获取待组装数据
        List<ItemDO> list = pageResult.getList();
        Collection<Long> itemCategoryIds = convertList(list, ItemDO::getCategoryId);
        Map<Long, ItemCategoryDO> itemCategoryDOMap = itemCategoryService.getItemCategoryMap(itemCategoryIds);
        Collection<Long> unitIds = convertList(list, ItemDO::getUnitId);
        Map<Long, UnitDO> unitDOMap = unitService.getUnitMap(unitIds);
        // 整合数据并返回
        List<ItemRespVO> respVOS = new ArrayList<>(list.size());
        list.forEach(itemDO -> {
            respVOS.add(ItemConvert.INSTANCE.convert(itemDO, itemCategoryDOMap.get(itemDO.getCategoryId()), unitDOMap.get(itemDO.getUnitId())));
        });
        return success(new PageResult<>(respVOS, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品通用信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:item:export')")
    @OperateLog(type = EXPORT)
    public void exportItemExcel(@Valid ItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemDO> list = itemService.getItemList(exportReqVO);
        // 导出 Excel
        List<ItemExcelVO> datas = ItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品通用信息.xls", "数据", ItemExcelVO.class, datas);
    }

}
